package com.villaday.idf.util;

import androidx.fragment.app.Fragment;
import androidx.appcompat.app.AppCompatActivity;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.base.BaseFragment;
import com.villaday.idf.base.BaseNoWhiteActivity;
import com.villaday.idf.custom.LoadingDialog;
import com.villaday.idf.custom.LoadingSaveDialog;
import com.villaday.idf.custom.pop.ShareActivityNoWhitePop;
import com.villaday.idf.custom.pop.ShareActivityPop;
import com.villaday.idf.custom.pop.SharePop;
import com.villaday.idf.model3.AllPostersEntity;

import static com.villaday.idf.custom.pop.SharePop.saveImageToSdCardXCX;
import static com.villaday.idf.custom.pop.SharePop.shareXCX;

public class ShareUtils {


    private static LoadingDialog loadingDialog;
    private static Gson gson = new GsonBuilder().create();

    public static void getData(final BaseFragment fragment, final AppCompatActivity activity, String type, String id) {//2 小区，3 房源 ，7 酒店，52 特卖(预售)

        loadingDialog = LoadingDialog.newInstance("加载中...");
        loadingDialog.show(activity.getFragmentManager());
        OkHttpUtil.getJSON(activity, String.format(Constant.URL.all_posters, type, id), new OkHttpUtil.OnDataListener() {
            @Override
            public void onResponse(String url, String json) {

                dismissLoading();
                if (!TextUtils.isEmpty(json)) {
                    LogeUtil.e("all_posters", json);
                    AllPostersEntity allPostersEntity = gson.fromJson(json, AllPostersEntity.class);
                    if (Constant.Strings.SUC.equals(allPostersEntity.getCode())) {//成功

                        SharePop sharePop = new SharePop(fragment,activity, activity, allPostersEntity.getBody());
                        sharePop.showPopupWindow();
                        fragment.mRxManager.post("sharepopEntity",allPostersEntity.getBody());

                    } else {
                        ToastUtil.initToast(activity, allPostersEntity.getMsg());
                    }
                }
            }


            @Override
            public void onFailure(String url, String error) {
                LogeUtil.e("all_posters", error);
                dismissLoading();

            }
        });

    }


    private static void dismissLoading() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    public static void getDataActivity(final BaseNoWhiteActivity activity, String type, String id) {//2 小区，3 房源 ，7 酒店，52 特卖(预售)

        loadingDialog = LoadingDialog.newInstance("加载中...");
        loadingDialog.show(activity.getFragmentManager());
        OkHttpUtil.getJSON(activity, String.format(Constant.URL.all_posters, type, id), new OkHttpUtil.OnDataListener() {
            @Override
            public void onResponse(String url, String json) {

                dismissLoading();
                if (!TextUtils.isEmpty(json)) {
                    LogeUtil.e("all_posters", json);
                    AllPostersEntity allPostersEntity = gson.fromJson(json, AllPostersEntity.class);
                    if (Constant.Strings.SUC.equals(allPostersEntity.getCode())) {//成功

                        ShareActivityPop sharePop = new ShareActivityPop(activity, activity, allPostersEntity.getBody());
                        sharePop.showPopupWindow();
                        activity.mRxManager.post("sharepopEntity",allPostersEntity.getBody());

                    } else {
                        ToastUtil.initToast(activity, allPostersEntity.getMsg());
                    }
                }
            }


            @Override
            public void onFailure(String url, String error) {
                LogeUtil.e("all_posters", error);
                dismissLoading();

            }
        });

    }
    public static void getDataActivityNoWhite(final BaseActivity activity, String type, String id) {//2 小区，3 房源 ，7 酒店，52 特卖(预售)

        loadingDialog = LoadingDialog.newInstance("加载中...");
        loadingDialog.show(activity.getFragmentManager());
        OkHttpUtil.getJSON(activity, String.format(Constant.URL.all_posters, type, id), new OkHttpUtil.OnDataListener() {
            @Override
            public void onResponse(String url, String json) {

                dismissLoading();
                if (!TextUtils.isEmpty(json)) {
                    LogeUtil.e("all_posters", json);
                    AllPostersEntity allPostersEntity = gson.fromJson(json, AllPostersEntity.class);
                    if (Constant.Strings.SUC.equals(allPostersEntity.getCode())) {//成功

                        ShareActivityNoWhitePop sharePop = new ShareActivityNoWhitePop(activity, activity, allPostersEntity.getBody());
                        sharePop.showPopupWindow();
                        activity.mRxManager.post("sharepopEntity",allPostersEntity.getBody());

                    } else {
                        ToastUtil.initToast(activity, allPostersEntity.getMsg());
                    }
                }
            }


            @Override
            public void onFailure(String url, String error) {
                LogeUtil.e("all_posters", error);
                dismissLoading();

            }
        });

    }


    public static void getDataForBieshu_xiaochengsun(final BaseFragment fragment, final AppCompatActivity activity, String type, String id) {//2 小区，3 房源 ，7 酒店，52 特卖(预售)

        loadingDialog = LoadingDialog.newInstance("加载中...");
        loadingDialog.show(activity.getFragmentManager());
        OkHttpUtil.getJSON(activity, String.format(Constant.URL.all_posters, type, id), new OkHttpUtil.OnDataListener() {
            @Override
            public void onResponse(String url, String json) {

                dismissLoading();
                if (!TextUtils.isEmpty(json)) {
                    LogeUtil.e("all_posters", json);
                    final AllPostersEntity allPostersEntity = gson.fromJson(json, AllPostersEntity.class);
                    if (Constant.Strings.SUC.equals(allPostersEntity.getCode())) {//成功
                        final AllPostersEntity.BodyBean body = allPostersEntity.getBody();

                       /* SharePop sharePop = new SharePop(fragment,activity, activity, allPostersEntity.getBody());
                        sharePop.showPopupWindow();
                        fragment.mRxManager.post("sharepopEntity",allPostersEntity.getBody());*/

                        final LoadingSaveDialog loadingDialog = LoadingSaveDialog.newInstance("");
                        loadingDialog.show(activity.getFragmentManager(), "1");
                        new Thread(new Runnable() {
                            @Override
                            public void run() {
                                saveImageToSdCardXCX(activity, body.getPosterList().get(0).getUrl());

                                shareXCX(body.getShare());
                                if (loadingDialog != null) {
                                    loadingDialog.dismiss();
                                }
                            }
                        }
                        ).start();

                    } else {
                        ToastUtil.initToast(activity, allPostersEntity.getMsg());
                    }
                }
            }


            @Override
            public void onFailure(String url, String error) {
                LogeUtil.e("all_posters", error);
                dismissLoading();

            }
        });

    }


}
